/**
 * 
 */
package net.sf.cldsql.sql2json.utl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.sf.cldsql.dj.container.DiContainer;
import net.sf.cldsql.dj.mvc.DiRoute;
import net.sf.cldsql.dj.mvc.DiService;
import net.sf.cldsql.dj.utl.DiException;

/**
 * SjServiceUtl
 */
public class SjServiceUtl {
    public static Object invoke(DiContainer container, DiRoute route) {
        try {
            DiService service = container.get(route.getService());
            String methodName = route.getMethod() != null ? route.getMethod() : DiService.DEFAULT_METHOD_NAME;
            Method method = service.getClass().getMethod(methodName, DiService.DEFAULT_METHOD_CLASS);
            return method.invoke(service, (Object[])route.getServiceArgs());
        } catch (SecurityException e) {
            throw new DiException(e);
        } catch (NoSuchMethodException e) {
            throw new DiException(e);
        } catch (IllegalArgumentException e) {
            throw new DiException(e);
        } catch (IllegalAccessException e) {
            throw new DiException(e);
        } catch (InvocationTargetException e) {
            throw new DiException(e);
        }
    }
}
